import { formatDate } from '@/utils/common'
import { Column } from '@/components/dynamic-table/types'
import { deleteResource } from '@/api/resource'

export const columns: Column[] = [
  {
    title: '序号',
    dataIndex: 'serial'
  },
  {
    title: '资料名称',
    dataIndex: 'resourceName'
  },
  {
    title: '资源类别',
    dataIndex: 'resourceType',
    slots: {
      customRender: 'resourceType'
    }
  },
  {
    title: '创建人',
    dataIndex: 'creator'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    slots: {
      customRender: 'createTime'
    },
    slotsType: 'format',
    slotsFunc: (val) => formatDate(val)
  },
  {
    title: '操作',
    dataIndex: 'action',
    slots: {
      customRender: 'action'
    },
    actions: [
      {
        type: 'text',
        text: '下载',
        style: {
          color: '#0077FF'
        },
        func: ({ record }) => {
          console.log(record)
        }
      },
      {
        type: 'popconfirm',
        text: '删除',
        style: {
          color: '#E70000'
        },
        func: ({ record }, refreshTableData) => {
          deleteResource(record.resourceId).then(() => {
            refreshTableData()
          })
        }
      }
    ]
  }
]

const codeMap = {
  '0': '报表共享',
  '1': '填报说明',
  '2': '制度通知',
  '3': '其他事项'
}
export function convertResourceTypeCode2Chinese(code: string) {
  return Reflect.has(codeMap, code) ? codeMap[code] : null
}

export function convertResourceTypeChinese2Code(chinese: string) {
  const obj: Record<string, any> = {}
  for (const [k, v] of Object.entries(codeMap)) {
    obj[v] = k
  }
  return chinese in obj ? obj[chinese] : null
  // return Object.prototype.hasOwnProperty.call(codeMap, chinese) ? codeMap[chinese] : null
}
